﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DMHelper.Models;

namespace DMHelper.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Accueil()
        {
            return View();
        }

        public ActionResult FeuillesPerso()
        {
            return View();
        }

        public ActionResult Feuille(int idFeuille)
        {
            DDSheet feuilleCourante = Models.DDSheet.LoadSheet(idFeuille);
            if (feuilleCourante == null)
            {
                return HttpNotFound();
            }
            ViewBag.IdFeuille = idFeuille;
            return View(idFeuille);
        }

        public ActionResult DMInfo()
        {
            List<DDSheet> listePerso = new List<DDSheet>();
            //List<PlayerIndex> list = DDSheet.playerSheetList();
            //foreach (var item in list)
            //{
            //    DDSheet perso = DDSheet.LoadSheet(Convert.ToInt32(item.ID));
            //    listePerso.Add(perso);
            //}
            return View(listePerso);
        }
        
        [HttpPost]
        public ActionResult DMInfo(string btnSubmit, FormCollection form)
        {
            switch (btnSubmit)
            {
                case "Nouvelle entrée":
                    OrdreInitiative.ajouter(form["name"], Convert.ToInt32(form["ini"]));
                    break;
                case "Modifier l'initiative":
                    OrdreInitiative.modifierIni(form["nouvelleIni"]);
                    break;
                case "Terminer le combat":
                    OrdreInitiative.supprimerTout();
                    break;
                default:
                    OrdreInitiative.supprimerString(btnSubmit);
                    break;
            }
            List<DDSheet> listePerso = new List<DDSheet>();
            return View(listePerso);
        }

        public ActionResult ModifFeuille(int idFeuille)
        {
            DDSheet feuilleAModif = Models.DDSheet.LoadSheet(idFeuille);
            if (feuilleAModif == null)
            {
                return HttpNotFound();
            }
            return View(feuilleAModif);
        }

        [HttpPost]
        public ActionResult SauvegarderModifs(DDSheet feuille)
        {
            if (ModelState.IsValid)
            {
                DDSheet nouvelleFeuille = feuille;
                Models.DDSheet.DestroySheet(feuille.SheetID);
                Models.DDSheet.SaveSheet(nouvelleFeuille);
                return RedirectToAction("Feuille", nouvelleFeuille.SheetID);
            }
            return View();
        }


        public ActionResult DeleteFeuille(int id)
        {
            DDSheet.DestroySheet(id.ToString());
            return View();
        }

    }
}
